/*
 * @features: 主要功能
 * @description: 内容说明
 * @Date: 2021-09-28 10:06:50
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-10-18 11:04:15
 * @LastEditors: judu233
 */

import { EDITOR } from "cc/env";
import c3d from "../../cc";
import ExtendsLoad from "../CCExtends";
import { StringFormat } from "./Util/StringFormat";

declare global {
    interface String {
        /**
         * @description 格式化字符串
         * @example
         * String.format("{0}-->{1}-->{2}","one","two","three") | String.format("{0}-->{1}-->{2}",["one","two","three"])
         * => "one-->two-->three"
         * */
        format(string, ...parem: any): string;
    }
    interface StringConstructor {

    }
}
class StringExtends {
    format() {
        var param = [];
        for (var i = 0, l = arguments.length; i < l; i++) {
            param.push(arguments[i]);
        }
        var statment = param[0]; // get the first element(the original statement)
        if (typeof statment != "string") {
            if (!EDITOR)
                c3d.error(`String.format error,first param is not a string`);
            return "";
        }
        param.shift(); // remove the first element from array
        if (Array.isArray(param[0]) && param.length == 1) {
            param = param[0];
        }
        return statment.replace(/\{(\d+)\}/g, function (m, n) {
            return param[n];
        });
    }

}


ExtendsLoad.load(String.prototype, StringExtends);
ExtendsLoad.load(String.prototype, StringFormat);